import { getPageRouteForm } from '../../../api/manage'
import create from 'dd-store'
import pageStore from '../../../stores/globalStore'
create.Page({
  store: pageStore,
  data: {
    options: [
    ],
    value: '',
    title: '',
    page: 1,
    hasMore: true, // 是否还有更多数据
    isLoading: false, // 是否正在加载中
    size: 10, //默认值10,
    oldPmFormId: ''
  },
  onLoad() {
    this.loadForm(true, 1)
  },
  // 加载数据
  loadForm(refresh = false, page) {
    dd.showLoading({
      content: '加载中...',
    });
    this.setData({
      isLoading: true
    })
    let data = {
      page,
      size: this.data.size,
    }
    getPageRouteForm(data).then(rs => {
      // console.log(rs, 'rs');
      let newList = rs.data.list.map((item, index) => {
        return Object.assign({}, { title: item.pmFormName }, { value: item.pmFormId })
      })
      const hasMore = newList.length >= this.data.size;
      dd.hideLoading();
      // console.log(refresh, newList);
      this.setData({
        options: refresh ? newList : this.data.options.concat(newList),
        isLoading: false,
        hasMore,
        page
      })
    })
  },
  // list-item选项的变化
  onChange(value, items, id) {

    this.setData({
      value,
      title: items.title,
      // pmId: items.value
    })
  },
  // 确认
  handleTap() {
    let pages = getCurrentPages();
    // console.log(pages[pages.length - 2].data.pmFormId);
    // console.log(this.$store.data.pmFormId);
    // console.log(this.$store.data);
    this.$store.data.selectedList = []
    this.$store.data.userOrGroup = 'no'
    this.update()


    // console.log(pages);
    // console.log(pages);
    if (pages.length > 1) {
      let prePage = pages[pages.length - 2];
      // let pmFormId = pages[pages.length - 2];
      // console.log(this.data.value);
      prePage.changeData(this.data.title, this.data.value);
      // prePage.changeData(this.data.value)
      // pmFormId.changeData(this.data.value)
      // console.log(prePage);
      dd.navigateBack();
    }
  },
  // 取消
  cancel() {
    this.setData({
      value: ''
    })
  },
  // 下拉刷新
  onReachBottom() {
    if (this.data.hasMore && !this.data.isLoading) {
      this.loadForm(false, this.data.page + 1)
    }
  }
})